package com.yuzhe.controller;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.yuzhe.model.Admin;
import com.yuzhe.service.AdminService;
import com.yuzhe.service.impl.AdminServiceImpl;
import com.yuzhe.utils.JSONResult;
import com.yuzhe.utils.MD5Utils;
import org.apache.commons.lang3.StringUtils;
import org.n3r.idworker.Sid;

import java.util.List;

public class AdminController extends Controller {

    private AdminService adminService = new AdminServiceImpl();

    //获取所有管理员列表
    public void getAdminList() {
        List<Admin> admins = Admin.dao.find("select * from admin");
        renderJson(JSONResult.ok(admins));
    }

    //保存管理员
    public void saveAdmin() {

        String username = getPara("username");
        String password = getPara("password");
        String checkPwd = getPara("checkPwd");

        if (StringUtils.isBlank(username)) {
            renderJson(JSONResult.errorMsg("用户名不能为空"));
            return;
        }

        if (StringUtils.isBlank(password)) {
            renderJson(JSONResult.errorMsg("密码不能为空"));
            return;
        }

        if (StringUtils.isBlank(checkPwd)) {
            renderJson(JSONResult.errorMsg("确认密码不能为空"));
            return;
        }

        if (password.length() < 5 || password.length() > 25) {
            renderJson(JSONResult.errorMsg("密码长度在5到25位之间"));
            return;
        }

        if (!password.equals(checkPwd)) {
            renderJson(JSONResult.errorMsg("再次输入的密码不一致"));
            return;
        }

        boolean b = adminService.checkUsernameIsExist(username);
        if (b) {
            renderJson(JSONResult.errorMsg("用户名已存在"));
        } else {
            try {
                String id = Sid.next();
                boolean save = Admin.dao.setId(id).setPassword(MD5Utils.getMD5Str(password)).setUsername(username).save();
                if (save) {
                    renderJson(JSONResult.ok());
                } else {
                    renderJson(JSONResult.errorMsg("添加失败"));
                }
            } catch (Exception e) {
                e.printStackTrace();
                renderJson(JSONResult.errorMsg("添加失败"));
            }
        }
    }

    public void deleteAdmin() {
        String ids = getPara("ids");
        if (StringUtils.isBlank(ids)) {
            renderJson(JSONResult.errorMsg("分类id不能为空"));
            return;
        }

        String sql = "delete from admin where id in (" + ids + ")";
        int update = Db.update(sql);
        if (update > 0) {
            renderJson(JSONResult.ok());
        } else {
            renderJson(JSONResult.errorMsg("批量删除失败"));
        }
    }

    public void updateAdmin() {
        String id = getPara("id");
        if (StringUtils.isBlank(id)) {
            renderJson(JSONResult.errorMsg("id不能为空"));
            return;
        }

        String username = getPara("username");
        String password = getPara("password");
        String checkPwd = getPara("checkPwd");

        if (StringUtils.isBlank(username)) {
            renderJson(JSONResult.errorMsg("用户名不能为空"));
            return;
        }

        if (StringUtils.isBlank(password)) {
            renderJson(JSONResult.errorMsg("密码不能为空"));
            return;
        }

        if (StringUtils.isBlank(checkPwd)) {
            renderJson(JSONResult.errorMsg("确认密码不能为空"));
            return;
        }

        if (password.length() < 5 || password.length() > 30) {
            renderJson(JSONResult.errorMsg("密码长度在5到25位之间"));
            return;
        }

        if (!password.equals(checkPwd)) {
            renderJson(JSONResult.errorMsg("再次输入的密码不一致"));
            return;
        }

        String currentPwd = "" ;
        //根据 id查出对应的帐号信息
        Admin admin = Admin.dao.findById(id);
        if (admin!=null){

            try {
                if (admin.getPassword().equals(password)){
                    currentPwd = admin.getPassword();
                } else {
                    currentPwd = MD5Utils.getMD5Str(password);
                }
                boolean result = Admin.dao.set("id", id).set("username", username)
                        .set("password", currentPwd).update();
                if (result) {
                    renderJson(JSONResult.ok());
                } else {
                    renderJson(JSONResult.errorMsg("更新失败"));
                }
            } catch (Exception e) {
                renderJson(JSONResult.errorMsg("更新失败"));
                e.printStackTrace();
            }

        } else {
            renderJson(JSONResult.errorMsg("没有找到对应的数据"));
        }


    }

}
